package cn.tedu.tmall.admin.mall.dao.persist.repository.impl;

import cn.tedu.tmall.admin.mall.dao.persist.mapper.CategoryMapper;
import cn.tedu.tmall.admin.mall.dao.persist.repository.ICategoryRepository;
import cn.tedu.tmall.admin.mall.pojo.entity.Category;
import cn.tedu.tmall.admin.mall.pojo.vo.CategoryListLtemVO;
import cn.tedu.tmall.admin.mall.pojo.vo.CategoryStandardVO;
import cn.tedu.tmall.common.pojo.vo.CategoryListVO;
import cn.tedu.tmall.common.pojo.vo.PageData;
import cn.tedu.tmall.common.util.PageInfoToPageDataConverter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CategoryRepositoryImpl implements ICategoryRepository {
    @Autowired
    private CategoryMapper mapper;
    @Override
    public int insert(Category category) {
        return mapper.insert(category);
    }

    @Override
    public int countByName(String name) {
        QueryWrapper<Category> wrapper=new QueryWrapper<>();
        wrapper.eq("name",name);
        return mapper.selectCount(wrapper);
    }

    @Override
    public CategoryStandardVO getStandardById(Long id) {
        return mapper.getStandardById(id);
    }

    @Override
    public int updateById(Category category) {
        return mapper.updateById(category);
    }

    @Override
    public PageData<CategoryListLtemVO> listByParent(Long parentId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<CategoryListLtemVO> list=mapper.listByParent(parentId);
        PageInfo<CategoryListLtemVO> pageInfo=new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    @Override
    public List<CategoryListVO> listByParentId(Long parentId) {
        return mapper.listByParentId(parentId);
    }
}
